<?php
/*正宗好食堂，第一时间为您送上金山食堂的菜单！
	1.本程序基于微博api(open.weibo.com/)创建，获得api授权的信息如下：
		授权开发者：李丰亚
		开发者注册邮箱：http://open.weibo.com/
		授权应用：金山食堂小喇叭
		权限级别：普通
	2.程序逻辑：
		通过调用微博api的接口(http://open.weibo.com/wiki/2/statuses/user_timeline)来获取微博用户 @金山食堂 的最近微博，
		然后对api返回内容进行判断和解析：内容辨认、检查是否已转发过、微博文本的加工、转发、记录转发。
*/

//api config
//获取用户微博的对应api接口地址
$api_address = 'http://api.weibo.com/2/statuses/user_timeline.json';

//调用接口传递的参数：screen_name为昵称'金山食堂'的url编码,access_token为api授权的登录认证key.
$send_args = array('source' => '2546437393',
			'uid' => '1803406960',);

//list message to
$message_list = array('15110249334');

/*
//api调用时的since_id参数设置.
$since_id = getSinceId();
$send_args['since_id'] = $since_id;
*/

//$api_method = 'GET';
$r = new HttpRequest($api_address, HttpRequest::METH_GET);
$r->addQueryData($send_args);
try{
	$r->send();
	if ($r->getResponseCode() == 200) {
		$api_return = $r->getResponseBody();		
	}
}catch (HttpException $ex) {
	echo $ex;
	die;
}

//微博列表
$wb_hash = json_decode($api_return);
$weibo_array = $wb_hash['status'];

//获取要转发的关键字
$keywords = getKeyWorkArray();

foreach($weibo_array as $weibo){
	//文本是否匹配转发条件
	$text = $weibo['text'];
	$keyword_match_tag = 0;
	foreach($keywords as $keyword){
		if(preg_match("/$keyword/", $text)){
			$keyword_match_tag = 1;
			break;
		}
	}

	//时间是否符合条件:考虑到会删微博、修改微博的情况，微博发布时间在10分钟内的暂不转发.
	$create_at = $weibo['create_at'];
	$interval = dateInterval($create_at);
	$interval_match_tag = ($interval >= 60 * 10) ? 1 : 0;

	//是否已转发过
	$wb_id = $weibo['id'];
	$hasBeenSent = checkSent($wb_id);

	if($keyword_match_tag == 1 && $interval_match_tag == 1 && $hasBeenSent == 0){
		//先插入发送记录,以免发送过程中其他的进程检查到没有发送记录
		$receive_num_string = join(',',$message_list);
		$sql = "insert into message_send (`wb_id`, `receive_num`, `status`) VALUES ('$wb_id','$receive_num_string','1')";
		mysql_query($sql) or die "sql '$sql' execute failed: " . mysql_error();

		//发送短信
		
	}
}

function checkSent($id){
	$sql = "select id from message_send where wb_id = '$id' and status = 1";
	$result = mysql_query($sql);
	$count = mysql_num_rows($result) or die "sql '$sql' query failed:" . mysql_error();
	return $count == 0 ? 0 : 1;
}

function dateInterval($create_time){
	if(! defined($create_time)){
		//未获取到发博时间则认为是刚发送,进而阻止短消息的发送
		return 0;
	}
	$interval_seconds = time() - strtotime($create_time);
	return $interval_seconds;
}

function getKeyWorkArray(){
	$keywords = array();
	$directory = dirname(__FILE__);
	$keyword_file = $directory . '/' . 'keyword.conf';
	$fh = fopen($keyword_file,'r') or die "couldn't open file $keyword_file";
	while(!feof($fh)){
		$line = fgets($fh);
		if(preg_match('/^\s+$/', $line) || preg_match('/^\s*\#/', $line)){
			continue;
		}
		print $line; //delete this line
		array_push($keywords, $line);
	}
	fclose($fh);
	return $keywords;
}

function getSinceId(){
	$sql = "select max(id) as max_id from max_id where status = 1;";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($request);
	$since_id = $row['max_id'];
	return $since_id;
}

?>


